<template>
  <el-dialog :visible.sync="dialogVisible" title="编辑">
    <el-form ref="form" :model="addForm" :rules="addRules" label-width="120px" size="small">
      <el-form-item label="行业" prop="accountsSystem">
        <el-input v-model="addForm.accountsSystem"/>
      </el-form-item>
      <el-form-item label="科目编码" prop="encode">
        <el-input v-model="addForm.encode"/>
      </el-form-item>
      <el-form-item label="科目名称" prop="name">
        <el-input v-model="addForm.name"/>
      </el-form-item>
      <el-form-item label="上级科目编码">
        <el-input v-model="addForm.parentEncode"/>
      </el-form-item>
      <el-form-item label="科目类别" prop="subType">
        <el-select v-model="addForm.subType" style="width: 100%" class="theme-select">
          <el-option v-for="(item, index) in $SUBJECT_CATEGORY_NEW_LIST" :key="index" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="余额方向" prop="balanceType">
        <el-radio v-for="(item,index) in $BALANCE_TYPE_LIST" :key="index" v-model="addForm.balanceType" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-checkbox v-model="addForm.assistCheck" true-label="1" false-label="0">辅助核算</el-checkbox>
        <el-form-item label="常用辅助" prop="commonAssistName">
          <el-input v-model="addForm.commonAssistName" :disabled="addForm.assistCheck === '0'" />
        </el-form-item>
        <el-form-item label="财政辅助" prop="financeAssistName">
          <el-input v-model="addForm.financeAssistName" :disabled="addForm.assistCheck === '0'" />
        </el-form-item>
        <el-form-item label="自定义辅助" prop="defineAssistName">
          <el-input v-model="addForm.defineAssistName" :disabled="addForm.assistCheck === '0'" />
        </el-form-item>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-row>
          <el-col :span="4">
            <el-checkbox v-model="addForm.amountCheck" true-label="1" false-label="0">数量核算</el-checkbox>
          </el-col>
          <el-col :span="20">
            <el-form-item label="计量单位" prop="unit">
              <el-input :disabled="addForm.amountCheck === '0'" v-model="addForm.unit"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-row>
          <el-col :span="4">
            <el-checkbox v-model="addForm.foreignCheck" true-label="1" false-label="0">外币核算</el-checkbox>
          </el-col>
          <el-col :span="20">
            <el-form-item label="辅助币别" prop="forListName">
              <el-input v-model="addForm.forListName" :disabled="addForm.foreignCheck === '0'" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="科目特征" prop="feature">
        <el-select v-model="addForm.feature" style="width: 100%" class="theme-select">
          <el-option v-for="(item, index) in $SUBJECT_FEATURE_LIST" :key="index" :label="item.label" :value="item.value" style="width: 100%"/>
        </el-select>
      </el-form-item>
      <el-form-item label="受控系统" prop="controSystem">
        <el-input v-model="addForm.controSystem"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="okHandler">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
function createSubjectForm(tar) {
  let raw = {
    encode: '',
    name: '',
    parentEncode: '',
    subType: '',
    balanceType: '1',
    assistCheck: '0',
    commonAssistName: '',
    amountCheck: '0',
    unit: '',
    foreignCheck: '0',
    forListName: '',
    feature: '',
    accountsSystem: '',
    defineAssistName: '',
    controSystem: '',
    financeAssistName: ''
  }
  if (tar) {
    raw = Object.assign(raw, tar)
  }
  return raw
}
export default {
  name: 'SubjectAddDialog',
  data() {
    return {
      dialogVisible: false,
      addForm: createSubjectForm(),
      dialogTitle: '',
      dialogType: 'add',
      addRules: {
        name: { required: true, message: '必填', trigger: 'blur', max: 20 }
      }
    }
  },
  computed: {
    ...mapGetters([
    ])
  },
  created() {
  },
  methods: {
    open(row) {
      this.addForm = createSubjectForm(row)
      this.dialogVisible = true
    },
    // 弹出框确认
    okHandler() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let url = 'account/subTmp/save'
          if (this.dialogType === 'edit') {
            url = 'account/subTmp/update'
          }
          this.$http.post(url, this.addForm).then(() => {
            this.$emit('ok')
            this.dialogVisible = false
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
